﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    class Utility : IDisposable
    {
        /*定数*/
        public const string ROUTE_FIELD = "SmUserId";
        public const string FEATURE = "feature_cd";
        public const string DAIBUN = "daibun_cd";
        public const string TSUUSHO = "tsuusho_cd";
        public const string ROUTE_DATASET_NAME = "線路L";
        public const string BUFFER_TITLE = "Buffer200_";
        public const string MAP_NAME = "埼京線（1km）";
        public const int BUFFER_ALARM_DISTANCE_2 = 200;
        public const string WORKSPAPCE_NAME = "防護無線鳴動総合エリアマップ.smw";
        //public const int BUFFER_ALARM_SMOOTH = 4;
        public const int MAX_UP_DOWN_DISTANCE = 50;
        //public const int MAX_ROUTE_PART_COUNT = 3000;
        //public const int PRECISION = 2;

        /*変数*/
        public static Hashtable routeLnWithBfr = null;
        public static Hashtable routeFtrWithLn = null;
        public static string datasourceAlias = string.Empty;
        public soDataSource objSource = null;
        private AxSuperMapLib.AxSuperWorkspace superWksp = null;
        public AxSuperMapLib.AxSuperMap routeMap = null;
        private string SDBPath = String.Empty;

        public Utility(AxSuperMapLib.AxSuperWorkspace superWksp, string SDBPath)
        {
            this.superWksp = superWksp;
            this.SDBPath = SDBPath;
            InitializeSource();
            SetHashPairInfo();
        }

        private void InitializeSource()
        {
            objSource = superWksp.OpenDataSource(
                SDBPath, datasourceAlias, seEngineType.sceSDBPlus, false);
            if (objSource == null)
            {
                throw new Exception("Initialize datasource failed!");
            }
            else
            {
                //
            }
        }

        public void InitializeMap(AxSuperMapLib.AxSuperMap objMap)
        {
            if (objSource.Connected)
            {
                objSource.Disconnect();
            }
            superWksp.Close();
            if (superWksp.Open(SDBPath.Substring(0, SDBPath.LastIndexOf('\\')) + @"\" + WORKSPAPCE_NAME))
            {
                objMap.Connect(superWksp.CtlHandle);
                objSource = superWksp.Datasources[1];

                if (!objMap.OpenMap(MAP_NAME))
                {
                    throw new Exception("Open map failed!");
                }
                //routeMap = objMap;
            }
            else
            {
                throw new Exception("Open workspace failed!");
            }
        }

        private void SetHashPairInfo()
        {
            routeLnWithBfr = new Hashtable();
            routeFtrWithLn = new Hashtable();
        }

        //public void ReleaseMemory()
        public void Dispose()
        {
            if (objSource.Connected)
            {
                objSource.Disconnect();
            }
            Marshal.ReleaseComObject(objSource);
            objSource = null;
            //Marshal.ReleaseComObject(routeMap);
            //routeMap = null;

            if (superWksp.Modified)
            {
                superWksp.Save();
            }
            superWksp.Close();

            GC.SuppressFinalize(this);

            MessageBox.Show("Release memory OK!");
        }
    }
}
